package com.feng.strategy
{
	import com.feng.ControllerCenter;
	import com.feng.behavior.BehaviorControler;
	import com.feng.data.StateData;
	import com.feng.debug.logger;
	import com.feng.model.Behavior;
	import com.feng.stateAnalysis.AttackOrderAnalysis;
	import com.feng.stateAnalysis.ThreeAnalysis;

	/**
	 * 策略管理
	 * @author 风之守望者
	 * 2013-3-29
	 */
	public class StrategyManager
	{
		/**
		 * 选择策略
		 */
		public function selectStrategy():void
		{
			currentStrategy = [];
			freeRideId = -1;

			switch (stateData.livingPlayers.length)
			{
				case 6:
					currentStrategy.push(StrategyLibrary.sixStrategy);
					break;
				case 5:
					currentStrategy.push(StrategyLibrary.fiveStrategy);
					break;
				case 4:
					currentStrategy.push(StrategyLibrary.fourStrategy);
					break;
				case 3:
					//分析形势
					threeStateAnalysis.analysis();
					//采取 三足鼎立策略
					currentStrategy.push(StrategyLibrary.threeStrategy);
					break;
				case 2:
					currentStrategy.push(StrategyLibrary.challenge);
					break;
				default:
					throw new Error("不可能出现这种情况！");
					break;
			}
			logger("【使用策略】\n" + JSON.stringify(currentStrategy));
		}

		/** 初始化攻击策略 */
		public function initAttackStrategy():void
		{
			for each (var strategy:* in currentStrategy)
			{
				var attackSetting:Array = strategy.attackSetting.concat();

				behaviorControler.attackSetting = new Vector.<Behavior>();
				var behaviorObj:*;
				while (attackSetting.length > 0)
				{
					behaviorObj = attackSetting.pop();
					behaviorControler.attackSetting.push(new Behavior(behaviorObj[0], behaviorObj[1]));
				}
				logger("初始化攻击策略," + behaviorControler.attackSetting.length + "," + JSON.stringify(behaviorControler.attackSetting));
			}


		}

		/** 初始化防御策略 */
		public function initDefenseStrategy():void
		{
			for each (var strategy:* in currentStrategy)
			{
				var defenseSetting:Array = strategy.defenseSetting.concat();

				behaviorControler.defenseSetting = new Vector.<Behavior>();
				var behaviorObj:*;
				while (defenseSetting.length > 0)
				{
					behaviorObj = defenseSetting.pop();
					behaviorControler.defenseSetting.push(new Behavior(behaviorObj[0], behaviorObj[1]));
				}
				logger("初始化防御策略," + behaviorControler.defenseSetting.length + "," + JSON.stringify(behaviorControler.defenseSetting));
			}
		}

		/*****************************************************************/
		private var controllerCenter:ControllerCenter;

		/** 当然使用策略 */
		private var currentStrategy:Array = [];

		/** 顺风车玩家编号 */
		private var freeRideId:int = -1;

		public function StrategyManager(controllerCenter:ControllerCenter)
		{
			this.controllerCenter = controllerCenter;
		}

		private function get behaviorControler():BehaviorControler
		{
			return controllerCenter.behaviorControler;
		}

		private function get stateData():StateData
		{
			return controllerCenter.stateData;
		}

		private function get threeStateAnalysis():ThreeAnalysis
		{
			return controllerCenter.threeStateAnalysis;
		}

		private function get attackOrderAnalysis():AttackOrderAnalysis
		{
			return controllerCenter.attackOrderAnalysis;
		}
	}
}
